package com.ciwong.mobilelib.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.GridView;

/**
 * 禁止滚动的GridView
 */
public class NoScrollGridView extends GridView
{

    private final int two = 2;
    /**
     * 内部禁止滑动的GridView
     *
     * @param context 上下文
     * @param attrs 属性
     */
    public NoScrollGridView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    /**
     * 内部禁止滑动的GridView
     *
     * @param context 上下文
     */
    public NoScrollGridView(Context context)
    {
        super(context);
    }

    /**
     * 内部禁止滑动的GridView
     * @param context 上下文
     * @param attrs 属性
     * @param defStyle 样式
     */
    public NoScrollGridView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> two,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev)
    {
        if (ev.getAction() == MotionEvent.ACTION_MOVE)
        {
            return true;// 禁止Gridview进行滑动
        }
        return super.dispatchTouchEvent(ev);
    }

}
